﻿Imports System.Windows.Forms
Imports System.Object
Imports System.Drawing.Drawing2D
Imports System.Net
Imports TNCBAL
Imports TNCDAL

Public Class ucFee
    Dim db As New TanNamChinhDataContext
    Dim m_fee As New CFee

    Private Sub ucFee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'KhoiTaoThuocTinhlvFee()
        KhoiTaoThuocTinhlvFeeType()
        KhoiTaolvFeeType()
        lvFeeType.TopItem.Selected = True
        lvFeeType.Items(1).Selected = True
        lvFeeType.Items(0).Selected = True
        KhoiTao_GiaoDien(True)
    End Sub

    Public Sub KhoiTaoThuocTinhlvFeeType()
        lvFeeType.MultiSelect = False
        lvFeeType.View = View.Details
        lvFeeType.BackColor = Color.White
        lvFeeType.FullRowSelect = True
        lvFeeType.Columns.Add("Loại Phí", 190)
        lvFeeType.HideSelection = False
    End Sub

    Public Sub KhoiTaodtgFee(ByVal id_FeeType As Integer, Optional ByVal NameUpdate As String = "")
        Dim fee = m_fee.SelectAllRecord(id_FeeType)
        dtgFee.DataSource = fee
        FormatdtgFee()

        Try
            If NameUpdate = "" Then
                dtgFee.Rows(0).Selected = True
                dtgFee.FirstDisplayedScrollingRowIndex = 0
            Else
                Dim index As Integer = 0
                For i As Integer = 0 To dtgFee.RowCount - 1
                    If dtgFee.Rows(i).Cells("FeeName").Value = NameUpdate Then
                        index = i
                        Exit For
                    End If
                Next

                dtgFee.Rows(index).Cells("FeeName").Selected = True
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Sub FormatdtgFee()
        dtgFee.RowHeadersWidth = 20
        dtgFee.RowHeadersDefaultCellStyle.ForeColor = Color.Blue

        dtgFee.MultiSelect = False
        dtgFee.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        dtgFee.BackgroundColor = Color.White
        dtgFee.ReadOnly = True
        dtgFee.AllowUserToAddRows = False
        dtgFee.AllowUserToDeleteRows = False
        With dtgFee

            .Columns("ID_Fee").Visible = False
            .Columns("ID_FeeType").Visible = False
            .Columns("IsActive").Visible = False
            .Columns("tblFeeType").Visible = False
            With .Columns("FeeName")
                .HeaderText = "Tên Phí"
                .Width = 200
            End With

            With .Columns("FeeAbbreviated")
                .HeaderText = "Tên viết tắt"
                .Width = 200
            End With

            With .Columns("IsBill")
                .HeaderText = "Is Bill"
                .Width = 90
            End With
        End With
    End Sub

    Public Sub KhoiTaolvFeeType()
        Dim db As New TanNamChinhDataContext
        Dim ft = From p In db.tblFeeTypes
                   Select p

        lvFeeType.Items.Clear()

        For Each p As tblFeeType In ft
            Dim item As New ListViewItem
            item.Text = p.FeeTypeName
            item.Name = p.ID_FeeType
            lvFeeType.Items.Add(item)
        Next
    End Sub

    Private Sub lvFeeType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvFeeType.SelectedIndexChanged
        Try
            Dim id_FeeType As Integer
            id_FeeType = lvFeeType.SelectedItems(0).Name
            KhoiTaodtgFee(id_FeeType)
        Catch ex As Exception

        End Try
    End Sub

#Region "Formatting dtgFee"
    Private Sub dtgFee_UserAddedRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs)
        dtgFee.Rows(e.Row.Index - 1).Cells("Status").Value = 1
        dtgFee.Rows(e.Row.Index - 1).Cells("ID_FeeType").Value = lvFeeType.SelectedItems(0).Name
    End Sub

    Private Sub dtgFee_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs)
        If e.Row.Cells("Status").Value = 0 Or e.Row.Cells("Status").Value = 2 Then
            e.Row.Cells("Status").Value = 3
            e.Cancel = True
        End If
    End Sub

    Private Sub dtgFee_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        If dtgFee.CurrentRow.Cells("Status").Value = 0 Then
            dtgFee.CurrentRow.Cells("Status").Value = 2
        End If
    End Sub

    Private Sub dtgFee_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)
        Dim f As Font = dtgFee.DefaultCellStyle.Font

        If dtgFee.Rows(e.RowIndex).Cells("Status").Value = 1 Then
            ' Add row
            dtgFee.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Black
            dtgFee.Rows(e.RowIndex).DefaultCellStyle.Font = New Font(f, FontStyle.Bold)
        ElseIf dtgFee.Rows(e.RowIndex).Cells("Status").Value = 2 Then
            'Update(row)
            dtgFee.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Blue
            dtgFee.Rows(e.RowIndex).DefaultCellStyle.Font = New Font(f, FontStyle.Italic)
        ElseIf dtgFee.Rows(e.RowIndex).Cells("Status").Value = 3 Then
            ' Del row
            dtgFee.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Red
            dtgFee.Rows(e.RowIndex).DefaultCellStyle.Font = New Font(f, FontStyle.Strikeout)

        End If

        '    If dtgFee.Columns(e.ColumnIndex).Name.Equals("Status", StringComparison.InvariantCultureIgnoreCase) Then

        '        If e.Value = "2" AndAlso e.Value IsNot DBNull.Value Then
        '            'Update row
        '            dtgFee.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Blue
        '            dtgFee.Rows(e.RowIndex).DefaultCellStyle.Font = New Font(f, FontStyle.Italic)
        '        ElseIf e.Value = "1" AndAlso e.Value IsNot DBNull.Value Then
        '            ' Add row
        '            dtgFee.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Black
        '            dtgFee.Rows(e.RowIndex).DefaultCellStyle.Font = New Font(f, FontStyle.Bold)
        '        ElseIf e.Value = "3" AndAlso e.Value IsNot DBNull.Value Then
        '            ' Del row
        '            dtgFee.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Red
        '            dtgFee.Rows(e.RowIndex).DefaultCellStyle.Font = New Font(f, FontStyle.Strikeout)
        '        End If
        '    End If
    End Sub
#End Region

#Region "Event drawSubItem lvFee"

    Private Sub lvFee_DrawColumnHeader(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs)
        e.DrawDefault = True
    End Sub

    Private Sub lvFee_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs)
        'e.DrawDefault = False

        e.DrawBackground()
        e.DrawFocusRectangle()
    End Sub

    Private Sub lvFee_DrawSubItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs)
        If (e.ItemState And ListViewItemStates.Selected) <> 0 Then
            e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds) 'item is highlighted
        End If

        ' Draw the image and text
        If e.ColumnIndex = 0 Then
            'Dim lvw As ExtraData = DirectCast(e.Item.Tag, ExtraData)
            Dim f1 As New Font("Arial", 10, FontStyle.Bold)
            Dim f2 As New Font("Arial", 8, FontStyle.Regular)
            Dim Rectf1 As RectangleF = New Rectangle(e.Bounds.X + 35, e.Bounds.Y + 10, e.Bounds.Width - 35, f1.Height)
            Dim Rectf2 As RectangleF = New Rectangle(e.Bounds.X + 35, e.Bounds.Y + 18, e.Bounds.Width - 35, f2.Height)
            Dim sf As New StringFormat
            sf.Alignment = StringAlignment.Near
            sf.Trimming = StringTrimming.EllipsisCharacter
            Dim bm As New Bitmap(Application.StartupPath + "/ship.png")

            e.Graphics.DrawString(e.SubItem.Text, f1, Brushes.Black, Rectf1, sf)
            e.Graphics.DrawImage(bm, e.Bounds.X + 1, e.Bounds.Y + 1, 32, 32)
            'e.Graphics.DrawString(lvw.MinorText, f2, Brushes.Black, Rectf2, sf)
        Else
            e.DrawDefault = True
        End If
    End Sub
#End Region

#Region "ToolStrip Phí"
    'Private Sub SuaPhiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SuaPhiToolStripMenuItem.Click
    '    'Sửa
    '    lvFee.LabelEdit = True
    '    lvFee.SelectedItems(0).BeginEdit()
    'End Sub

    'Private Sub ThemPhiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThemFeeToolStripMenuItem.Click
    '    'Thêm
    '    ' Add row
    '    Dim db As New TanNamChinhDataContext
    '    Dim id_FeeType As Integer
    '    id_FeeType = lvFeeType.SelectedItems(0).Name

    '    Dim fee As New tblFee
    '    fee.FeeName = "Phí mới"
    '    fee.ID_FeeType = id_FeeType
    '    fee.FeeAbbreviated = ""
    '    db.tblFees.InsertOnSubmit(fee)
    '    db.SubmitChanges()

    '    KhoiTaolvFee(id_FeeType)
    '    lvFee.LabelEdit = True
    '    lvFee.Items(lvFee.Items.Count - 1).BeginEdit()
    'End Sub

    'Private Sub XoaPhiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XoaFeeToolStripMenuItem.Click
    '    'Xóa
    '    Dim db As New TanNamChinhDataContext
    '    Dim ID_Fee As Integer = lvFee.SelectedItems(0).Name
    '    ' Del row
    '    Dim fee = From p In db.tblFees
    '               Where p.ID_Fee = ID_Fee
    '               Select p
    '    db.tblFees.DeleteAllOnSubmit(fee)
    '    db.SubmitChanges()
    '    '
    '    Dim id_FeeType As Integer
    '    id_FeeType = lvFeeType.SelectedItems(0).Name
    '    KhoiTaolvFee(id_FeeType)
    'End Sub

    'Private Sub ChonPhiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChonPhiToolStripMenuItem.Click

    'End Sub

    'Private Sub lvFee_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.LabelEditEventArgs)
    '    If e.Label = Nothing Then
    '        Return
    '    End If

    '    'Update Fee
    '    Dim db As New TanNamChinhDataContext
    '    Dim ID_Fee As Integer = lvFee.SelectedItems(0).Name
    '    ' Update row
    '    Dim fee = (From p In db.tblFees
    '               Where p.ID_Fee = ID_Fee
    '               Select p).Single
    '    fee.FeeName = e.Label
    '    db.SubmitChanges()
    'End Sub
#End Region

#Region "ToolStrip Loại Phí"
    Private Sub ThemLoaiPhiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ThemFeeTypeToolStripMenuItem.Click
        'Thêm
        ' Add row
        Dim db As New TanNamChinhDataContext

        Dim feeType As New tblFeeType
        feeType.FeeTypeName = "Loại phí mới"
        db.tblFeeTypes.InsertOnSubmit(feeType)
        db.SubmitChanges()

        KhoiTaolvFeeType()
        lvFeeType.LabelEdit = True
        lvFeeType.Items(lvFeeType.Items.Count - 1).BeginEdit()
    End Sub

    Private Sub XoaLoaiPhiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles XoaLoaiPhiToolStripMenuItem.Click
        'Xóa
        Dim db As New TanNamChinhDataContext
        Dim id_FeeType As Integer
        id_FeeType = lvFeeType.SelectedItems(0).Name
        ' Del row
        Dim feeType = From p In db.tblFeeTypes
                   Where p.ID_FeeType = id_FeeType
                   Select p
        db.tblFeeTypes.DeleteAllOnSubmit(feeType)
        db.SubmitChanges()
        '
        KhoiTaolvFeeType()
    End Sub

    Private Sub SửaHãngTàuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SuaLoaiPhiToolStripMenuItem.Click
        'Sửa
        lvFeeType.LabelEdit = True
        lvFeeType.SelectedItems(0).BeginEdit()
    End Sub

    Private Sub lvFeeType_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.LabelEditEventArgs)
        If e.Label = Nothing Then
            Return
        End If

        'Update FeeType
        Dim db As New TanNamChinhDataContext
        Dim ID_FeeType As Integer = lvFeeType.SelectedItems(0).Name
        ' Update row
        Dim feeType = (From p In db.tblFeeTypes
                   Where p.ID_FeeType = ID_FeeType
                   Select p).Single
        feeType.FeeTypeName = e.Label
        db.SubmitChanges()
    End Sub

#End Region

#Region "Drap Drop lvFee Item"
    'Private Sub lv_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles lvFeeType.ItemDrag
    '    Dim myItem As ListViewItem
    '    Dim myItems(sender.SelectedItems.Count - 1) As ListViewItem
    '    Dim i As Integer = 0

    '    ' Loop though the SelectedItems collection for the source.
    '    For Each myItem In sender.SelectedItems
    '        ' Add the ListViewItem to the array of ListViewItems.
    '        myItems(i) = myItem
    '        i = i + 1
    '    Next
    '    ' Create a DataObject containg the array of ListViewItems.
    '    sender.DoDragDrop(New  _
    '    DataObject("System.Windows.Forms.ListViewItem()", myItems), _
    '    DragDropEffects.Move)
    'End Sub

    'Private Sub lv_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lvFeeType.DragDrop
    '    Dim myItem As ListViewItem
    '    Dim myItems() As ListViewItem = e.Data.GetData("System.Windows.Forms.ListViewItem()")
    '    Dim i As Integer = 0

    '    For Each myItem In myItems
    '        ' Add the item to the target list.
    '        'sender.Items.Add(myItems(i).Text)

    '        ' Remove the item from the source list.
    '        If sender Is lvFee Then
    '            'ListView2.Items.Remove(ListView2.SelectedItems.Item(0))
    '        Else
    '            'ListView1.Items.Remove(ListView1.SelectedItems.Item(0))

    '            'Dim srcItem As ListViewItem = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)
    '            Dim pt As Point
    '            Dim dest As ListViewItem
    '            pt = CType(sender, ListView).PointToClient(New Point(e.X, e.Y))
    '            dest = CType(sender, ListView).GetItemAt(pt.X, pt.Y)

    '            'update ID_FeeType 
    '            Dim db As New TanNamChinhDataContext
    '            Dim ID_FeeType As Integer = dest.Name
    '            Dim ID_Fee As Integer = myItem.Name

    '            ' Update row
    '            Dim fee = (From p In db.tblFees
    '                       Where p.ID_Fee = ID_Fee
    '                       Select p).Single
    '            fee.ID_FeeType = ID_FeeType
    '            db.SubmitChanges()

    '            KhoiTaolvFee(ID_FeeType)
    '        End If
    '        i = i + 1
    '    Next
    'End Sub

    'Private Sub lv_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lvFeeType.DragEnter
    '    ' Check for the custom DataFormat ListViewItem array.
    '    If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()") Then
    '        e.Effect = DragDropEffects.Move
    '    Else
    '        e.Effect = DragDropEffects.None
    '    End If
    'End Sub
#End Region

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For i As Integer = 0 To dtgFee.Rows.Count - 1
            Dim ID_Fee As Integer
            ID_Fee = dtgFee.Rows(i).Cells("ID_Fee").Value
            If dtgFee.Rows(i).Cells("Status").Value = 1 Then
                ' Add row
                Dim fee As New tblFee
                fee.FeeName = dtgFee.Rows(i).Cells("FeeName").Value
                fee.FeeAbbreviated = dtgFee.Rows(i).Cells("FeeAbbreviated").Value
                fee.ID_FeeType = dtgFee.Rows(i).Cells("ID_FeeType").Value
                db.tblFees.InsertOnSubmit(fee)
                db.SubmitChanges()
            ElseIf dtgFee.Rows(i).Cells("Status").Value = 2 Then
                ' Update row
                Dim fee = (From p In db.tblFees
                           Where p.ID_Fee = ID_Fee
                           Select p).Single
                fee.FeeName = dtgFee.Rows(i).Cells("FeeName").Value
                fee.FeeAbbreviated = dtgFee.Rows(i).Cells("FeeAbbreviated").Value
                db.SubmitChanges()
            ElseIf dtgFee.Rows(i).Cells("Status").Value = 3 Then
                ' Del row
                Dim fee = From p In db.tblFees
                           Where p.ID_Fee = ID_Fee
                           Select p
                db.tblFees.DeleteAllOnSubmit(fee)
                db.SubmitChanges()
            End If
        Next
        MsgBox("Đã lưu vào dữ liệu")
        Dim id_FeeType As Integer
        id_FeeType = lvFeeType.SelectedItems(0).Name
        KhoiTaodtgFee(id_FeeType)
    End Sub


#Region "btn Them, Xoa, Sua"
    Dim f_Them As Boolean = False
    Dim f_Sua As Boolean = False

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        txtFee.Text = ""
        txtFeeAbr.Text = ""
        chkIsBill.Checked = False


        Dim new_rank = (From p In db.tblFees
                   Where p.ID_FeeType = lvFeeType.SelectedItems(0).Name And p.IsActive = True
                   Select p.Rank).Max
        txtRank.Text = new_rank + 1

        txtFee.Focus()
        KhoiTao_GiaoDien(False)
        f_Them = True
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        KhoiTao_GiaoDien(False)
        txtFee.Focus()
        f_Sua = True
    End Sub

    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        Dim ID_Fee As Integer = dtgFee.CurrentRow.Cells("ID_Fee").Value
        'kiem tra truoc khi xoa
        Dim FeeName As String = dtgFee.CurrentRow.Cells("FeeName").Value
        If (MsgBox("Bạn muốn xóa " + FeeName + " ra khỏi danh sách", MsgBoxStyle.YesNo, "Chú ý") = MsgBoxResult.Yes) Then
            Try
                m_fee.Delete(ID_Fee)
                Dim ID_FeeType As Integer = lvFeeType.SelectedItems(0).Name
                KhoiTaodtgFee(ID_FeeType, "")
                'write systemlog
                Dim objLogController As New LogController
                objLogController.Insert(enumAction.Delete, String.Format("Fee Name {0}", FeeName))
            Catch ex As Exception
                MsgBox("Del - Phí: " + FeeName + " đang được sử dụng. Không xóa được")
            End Try
        End If
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        Dim ID_FeeType As Integer
        ID_FeeType = lvFeeType.SelectedItems(0).Name

        Dim obj_fee As New tblFee
        obj_fee.FeeName = txtFee.Text
        obj_fee.FeeAbbreviated = txtFeeAbr.Text
        obj_fee.IsBill = chkIsBill.Checked
        obj_fee.ID_FeeType = ID_FeeType

        Try
            obj_fee.Rank = txtRank.Text
        Catch ex As Exception
            obj_fee.Rank = 0
        End Try



        If f_Them Then
            obj_fee.IsActive = True
            m_fee.Insert(obj_fee)
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Add, String.Format("Fee Name {0}", obj_fee.FeeName))
            KhoiTaodtgFee(ID_FeeType, obj_fee.FeeName)
        End If

        If f_Sua Then
            obj_fee.ID_Fee = dtgFee.CurrentRow.Cells("ID_Fee").Value
            m_fee.Update(obj_fee)
            'write systemlog
            Dim objLogController As New LogController
            objLogController.Insert(enumAction.Edit, String.Format("Fee Name {0}", obj_fee.FeeName))
            Dim index As Integer = dtgFee.CurrentRow.Index
            KhoiTaodtgFee(ID_FeeType, obj_fee.FeeName)
        End If

        f_Them = False
        f_Sua = False
        KhoiTao_GiaoDien(True)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        KhoiTao_GiaoDien(True)
        Dim ID_FeeType As Integer = lvFeeType.SelectedItems(0).Name
        KhoiTaodtgFee(ID_FeeType, txtFee.Text)
        f_Them = False
        f_Sua = False
    End Sub

    Public Sub KhoiTao_GiaoDien(ByVal flag As Boolean)
        btnAdd.Enabled = flag
        btnEdit.Enabled = flag
        btnDel.Enabled = flag
        btnUpdate.Enabled = Not flag
        btnCancel.Enabled = Not flag
        lvFeeType.Enabled = flag
        dtgFee.Enabled = flag
        txtFee.ReadOnly = flag
        txtFeeAbr.ReadOnly = flag

        'If flag Then
        '    txtFee.BackColor = Color.Azure
        '    txtFeeAbr.BackColor = Color.Azure
        'Else
        '    txtFee.BackColor = Color.White
        '    txtFeeAbr.BackColor = Color.White
        'End If
    End Sub
#End Region

    Private Sub dtgFee_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgFee.CurrentCellChanged
        Try
            txtFee.Text = dtgFee.CurrentRow.Cells("FeeName").Value
            txtFeeAbr.Text = dtgFee.CurrentRow.Cells("FeeAbbreviated").Value
            chkIsBill.Checked = dtgFee.CurrentRow.Cells("IsBill").Value
            txtRank.Text = dtgFee.CurrentRow.Cells("Rank").Value
        Catch ex As Exception

        End Try
    End Sub

    
End Class

